public class Improvise implements Runnable {

	Object[] noteArray = new Object[2];
	int[] durArray;

	public void run() {
		System.out.println("IMPROVISE");
		int responseType = 0;
		int lastRow = Main.pchIn.length-2; // find last row (last phrase) ...
												// -2 because we just added one
		durArray = toDurations(Main.rhyIn[lastRow]);
		switch (responseType) {
		case (0): // playback
			for (int i = 0; i < Main.pchIn[lastRow].length; i++) {
				noteOut(Main.pchIn[lastRow][i],100,durArray[i]);			
			}
			break;
		}
		
		Main.humanPlaying = true;
		UIActions.setButton(true, "human");
	}

	void noteOut(int note, int vel, int wait) {
		noteArray[0] = note;
		noteArray[1] = vel;

		System.out.println("------generated note:" + noteArray[0] + " "
				+ noteArray[1] + " " + wait);
		OSC.main(noteArray, "/mididata3");
		try {
			Thread.sleep(wait);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	void waitSym(double wait) // wait in milliseconds
	{
		long waitMS = (long) (wait * 60000 / (Main.bpm * 480));
		try {
			Thread.sleep(waitMS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	int[] toDurations(int[] array) { // change onsets array into durations array (first diff)
		int[] durArray = new int[array.length-1];
		for (int i=0;i<array.length-1;i++) {
			durArray[i]=array[i+1]-array[i];
		}
		return durArray;
	}
}
